package org.sky.spring.common.service.zk;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;

public class ZKServiceImpl {
	private static Logger log = LoggerFactory.getLogger(ZKServiceImpl.class);
	private static final int SESSION_TIMEOUT = 30000;
	private String zkHost;
	private String root;
	private String serviceName;
	private String serviceHost;
	private ZooKeeper zooKeeper;
	public String getZkHost() {
		return zkHost;
	}
	public void setZkHost(String zkHost) {
		this.zkHost = zkHost;
	}
	public String getRoot() {
		return root;
	}
	public void setRoot(String root) {
		this.root = root;
	}
	public String getServiceName() {
		return serviceName;
	}
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}
	public String getServiceHost() {
		return serviceHost;
	}
	public void setServiceHost(String serviceHost) {
		this.serviceHost = serviceHost;
	}
	public void init() throws Exception{
		if(zkHost==null){
			throw new RuntimeException("zkHost is null");
		}
		try {
			zooKeeper = new ZooKeeper(zkHost, SESSION_TIMEOUT, null);
			zooKeeper.create(root + serviceName, serviceHost.getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		} catch (Exception e) {
			log.info(e.getMessage());
		}
		
	}
}
